package com.ruoyi.dynamic.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.CommonUtils;
import com.ruoyi.dynamic.mapper.RoomNoticeMapper;
import com.ruoyi.dynamic.model.constant.RoomChatRoomKvConstant;
import com.ruoyi.dynamic.model.pojo.entity.Room;
import com.ruoyi.dynamic.model.pojo.entity.RoomNotice;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomNoticeAddOrEditRequest;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomNoticePushRequest;
import com.ruoyi.dynamic.service.IMessageService;
import com.ruoyi.dynamic.service.IRoomNoticeService;
import com.ruoyi.dynamic.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 房间公告 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2024-11-28
 */
@Service
public class RoomNoticeServiceImpl extends ServiceImpl<RoomNoticeMapper, RoomNotice> implements IRoomNoticeService {

    @Autowired
    private IRoomService roomService;
    @Autowired
    private IMessageService iMessageService;

    @Override
    public List<RoomNotice> listPage(Long roomId, Long userId) {
        roomService.verify(roomId);
        return lambdaQuery()
                .eq(RoomNotice::getRoomId, roomId)
                .list();
    }

    @Override
    public boolean addOrEdit(AppRoomNoticeAddOrEditRequest request, Long userId) {
        Room verify = roomService.verify(request.getRoomId());
        Assert.isTrue(verify.getUserId().equals(userId), "您不是此主播无法操作");
        RoomNotice one = getById(request.getRoomNoticeId());
        if (CommonUtils.isNull(one)) {
            one = new RoomNotice();
            one.setCreateTime(new Date());
        }
        one.setUserId(userId);
        one.setRoomId(request.getRoomId());
        one.setContent(request.getContent());
        one.setTitle(request.getTitle());
        return saveOrUpdate(one);
    }

    @Override
    public boolean del(Long id, Long userId) {
        RoomNotice roomNotice = getById(id);
        Assert.notNull(roomNotice, "此公告不存在");
        Assert.isTrue(roomNotice.getUserId().equals(userId), "您无权限操作");
        return removeById(id);
    }

    @Override
    public void publish(AppRoomNoticePushRequest request, Long userId) {
        Room verify = roomService.verify(request.getRoomId());
        RoomNotice roomNotice= getById(request.getId());
        iMessageService.setRoomChatRoomAttr(verify.getGroupId(),verify.getUserId().toString(),
                RoomChatRoomKvConstant.ROOM_NOTICE,roomNotice.getContent(),0,"","");
    }

}
